/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.pconline.autoclub.web.wap;

import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.Pager;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

/**
 * 车友会WAP版工具类
 * @author xulin
 */
public class ActionKit {
    
    public  static final int DEF_PAGE_NUM  = 1;
    public static final int DEF_PAGE_SIZE = 10;
    public static final int MAX_PAGE_SIZE = 100;
    public static final int MIN_PAGE_SIZE = 1;
    public static final String SNED_ERROR_404 = "redirect:http://m.pcauto.com.cn/x/404/";
    /**
     * 成功状态码
     */
    private static final int STATUS_OK = 0;
    /**
     * 失败状态码
     */
    private static final int STATUS_FAIL = -1;
    
    protected static final org.slf4j.Logger log = LoggerFactory.getLogger(ActionKit.class);
    public static void printSuccessMsg(HttpServletResponse response,JSONObject obj,String callback){
        if(obj == null){
            printMsg(response, "", callback);
            return;
        }
        obj.put("code", STATUS_OK);
        printMsg(response, obj.toJSONString(), callback);
    }
    public static void printSuccessMsg(HttpServletResponse response,String msg,String callback){
        JSONObject obj = new JSONObject();
        obj.put("code", STATUS_OK);
        obj.put("message", msg);
        printMsg(response, obj.toJSONString(), callback);
    }
    public static void printErrorMsg(HttpServletResponse response,String msg,String callback){
        JSONObject obj = new JSONObject();
        obj.put("code", STATUS_FAIL);
        obj.put("message", msg);
        printMsg(response, obj.toJSONString(), callback);
    }
    private static void printMsg(HttpServletResponse response,String msg,String callback){
        try {
            PrintWriter printWriter = response.getWriter();
            if(StringUtils.isBlank(callback)){
                printWriter.println(msg);
            }else{
                printWriter.println(EncodeUtils.encodeForHTML(callback)+"("+msg+")");
            }
        } catch (IOException ex) {
            Logger.getLogger(ActionKit.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void sendLogin(String url,HttpServletResponse response){
        try {
            response.sendRedirect(url);
        } catch (IOException ex) {
            Logger.getLogger(ActionKit.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void putPagerMessage(JSONObject obj,Pager pager){
        if(obj!=null){
            int pageNo = 0;
            int pageSize = 0;
            int pagerCount = 0;
            int total = 0 ;
            if(pager!=null){
                pageNo = pager.getPageNo();
                pageSize = pager.getPageSize();
                pagerCount = pager.getPageCount();
                total = pager.getTotal();
            }
            obj.put("pageNo", pageNo);
            obj.put("pageSize", pageSize);
            obj.put("pageCount", pagerCount);
            obj.put("total", total);
        }
    }
}
